<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />  
        <title>Railway Official Website</title>
        <link rel="stylesheet" href="css/jquery-ui.css"/>
        <link rel="stylesheet" href="css/foundation.css"/>
        <link rel="stylesheet" href="css/normalize.css"/>
        <link rel="stylesheet" href="css/footable/footable.core.css"/>
        <link rel="stylesheet" href="css/pagination.css"/>
        <script src="js/vendor/jquery.js"></script>
        <script src="js/ui/jquery-ui.js"></script>
        <script src="js/vendor/custom.modernizr.js"></script>
        <script src="js/footable/footable.js"></script>
        <script src="js/footable/footable.paginate.js"></script>
        <script src="js/viewticket.js"></script>
        <?php
        session_start();
        ?>
        <script>
            $(document).ready(function() {
                $('.footable').footable();
            });
        </script>
    </head>
    <body>
        <div id="myModal" class="reveal-modal medium" style="height: 500px;">
            <form class="custom" id="seatform">
                <input type="hidden" id="ticketnumber"/>
                <input type="hidden" name="action" value="getCoachmap"/>
                <div class="row">
                    <div class="row">
                        <div class="small-3 columns">
                            <label for="coach" class="inline">Select Coach </label>
                        </div>
                        <div class="small-4 left columns">
                            <select id="coach" name="coach" onchange="getCoachmap();">
                                <option value="0">Select Coach</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div id="coachmap" class="row" style="height: 415px; overflow: auto;">

                        </div>
                    </div>
                </div>
            </form>
            <a class="close-reveal-modal">&#215;</a>
        </div>
        <div id='overlay' style="display:none; opacity: 0.4;position: fixed;top: 0;left: 0;background-color: black;width: 100%;z-index: 5000;">
            <img src="img/loader.gif" style="opacity:1; position: absolute; top: 50%;left: 50%;margin-left:-16px;
                 margin-top:-16px';"></img>
        </div>
        <div>
            <nav class="top-bar">
                <ul class="title-area">
                    <li class="name" onClick="void(0);">
                        <h1><a href="#">Railway Official Website</a></h1>
                    </li>
                    <li class="divider"></li>
                    <li class="toggle-topbar menu-icon">
                        <a href="#"><span>menu</span></a>
                    </li>
                </ul>
                <section class="top-bar-section">
                    <ul class="right">
                        <li class="divider"></li>
                        <li><a href="index.php">Home</a></li>
                        <li class="divider"></li>
                        <li><a href="index.php">Forum</a></li>
                        <li class="divider"></li>
                        <?php
                        if (isset($_SESSION['username'])) {
                            echo '<li><a href="profile.php?mode=21">My profile</a></li>';
                            echo '<li class="divider"></li>';
                            echo '<li><a href="logout.php">Logout</a></li>';
                            echo '<li class="divider"></li>';
                        }
                        else
                            echo '<li><a href="loginPage.php">Login</a></li>';
                        echo '<li class="divider"></li>';
                        ?>
                    </ul>
                </section>
            </nav>
        </div>
        <div class="row">
            <?php
            if (isset($_SESSION['username']) && isset($_GET['mode'])) {
                $mode = $_GET['mode'];
                ?>
                <div class="columns">
                    <div class="row">
                        <div class="large-9 push-3 columns">
                            <?php
                            require_once 'classes/Member.php';
                            require_once 'classes/Ticket.php';
                            $m = new Member();
                            $t = new Ticket();
                            $icno = $m->getMemberIC($_SESSION['uid']);
                            switch ($mode) {
                                case 'view':
                                    $referenceno = trim($_GET['referenceno']);
                                    $ticket = $t->viewOneTicket($referenceno, $icno);
                                    if ($ticket) {
                                        ?>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">
                                                        <h4>Ticket details</h4>
                                                        <hr/>
                                                        <div class="row">
                                                            <div class="large-7 columns">
                                                                <table style="width: 100%">
                                                                    <tr>
                                                                        <td>Reference No.</td>
                                                                        <td>:</td>
                                                                        <td><?php echo str_pad($ticket['referenceno'], 10, '0', STR_PAD_LEFT); ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Date Purchased</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['purchasedate']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Departure Date</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['departuredate']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Route</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['routename']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Departure Time</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['departure']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Arrive Time</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['arrival']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Origin</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['origin']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Destination</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['destin']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Seat</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['coach'] . ' - ' . $ticket['seatno']; ?></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Status</td>
                                                                        <td>:</td>
                                                                        <td><?php echo $ticket['status']; ?></td>
                                                                    </tr>
                                                                </table>
                                                            </div>
                                                            <div class="large-5 columns panel">
                                                                <p>Ticket's QR Code</p>
                                                                <img src="qrgenerator.php?param=<?php echo $ticket['code'] ?>"/>
                                                            </div>
                                                            <div class="large-12 columns">
                                                                <div class="row">
                                                                    <?php
                                                                    if ($ticket['status'] == 'AMD' || $ticket['status'] == 'CCL' || $ticket['status'] == 'CHK' || strtotime($ticket['departuredate']) < time()) {
                                                                        ?>
                                                                        <div class="large-2 columns">
                                                                            <input type="button" value="Amend" class="small button left" disabled/>
                                                                        </div>
                                                                        <div class="large-3 columns">
                                                                            <input type="button" value="Cancel & Refund" class="small button left" disabled/>
                                                                        </div>
                                                                        <?php
                                                                    } else {
                                                                        ?>
                                                                        <div class="large-2 columns">
                                                                            <input type="button" value="Amend" class="small button left" 
                                                                                   onclick="amendTicket('<?php echo $ticket['referenceno']; ?>', '<?php echo $ticket['routename']; ?>', '<?php echo $ticket['origin']; ?>', '<?php echo $ticket['destin']; ?>');"/>
                                                                        </div>
                                                                        <div class="large-3 columns">
                                                                            <input type="button" value="Cancel & Refund" class="small button left" 
                                                                                   onclick="cancelTicket('<?php echo $ticket['referenceno']; ?>');"/>
                                                                        </div>
                                                                        <?php
                                                                    }
                                                                    ?>
                                                                    <div class="large-7 columns">
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                    } else {
                                        ?>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">
                                                        <h4>Ticket details</h4>
                                                        <hr/>
                                                        <h5 class="subheader">Ticket not found. Please try again with a valid ticket reference number.</h5>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    break;
                                case 'viewall':
                                    $ticket = $t->viewAllPurchasedTicket($icno);
                                    ?>
                                    <div class="row">
                                        <div class="large-11 large-offset-1 columns">
                                            <div class="row">
                                                <?php
                                                if ($ticket) {
                                                    ?>
                                                    <div class="large-12 columns">
                                                        <h4>Ticket history</h4>
                                                        <h6 class="subheader">You can click on each ticket for more details.</h6>
                                                        <hr/>

                                                        <div class="row">
                                                            <div class="large-3 columns">
                                                                <label for="referenceno" class="inline">Search for ticket :</label>
                                                            </div>
                                                            <div class="large-7 columns">
                                                                <input type="text" id="referenceno" placeholder="Enter ticket reference number without leading zeros"/>
                                                                <small id="referenceErr" class="error" style="display: none"></small>
                                                            </div>
                                                            <div class="large-2 columns">
                                                                <input type="button" value="Search" class="small button" onclick="searchTicket();"/>
                                                            </div>
                                                        </div>

                                                    </div>
                                                    <div class="large-12 columns">
                                                        <table class='footable'>
                                                            <thead>
                                                                <th style="width: 130px">Reference No</th>
                                                                <th data-hide='phone'>Class</th>
                                                                <th data-hide='phone'>Origin</th>
                                                                <th data-hide='phone'>Destination</th>
                                                                <th data-hide='phone'>Departure</th>
                                                                <th>Date Purchased</th>
                                                            </thead>
                                                            <tbody>
                                                                <?php
                                                                for ($i = 0; $i < count($ticket); $i++) {
                                                                    echo '<tr>' .
                                                                    '<td><a href="viewticket.php?mode=view&referenceno=' . $ticket[$i]['referenceno'] . '">' . str_pad($ticket[$i]['referenceno'], 10, '0', STR_PAD_LEFT) . '</a></td>' .
                                                                    '<td>' . $ticket[$i]['class'] . '</td>' .
                                                                    '<td>' . $ticket[$i]['origin'] . '</td>' .
                                                                    '<td>' . $ticket[$i]['destination'] . '</td>' .
                                                                    '<td>' . $ticket[$i]['departuredate'] . ' ' . $ticket[$i]['departure'] . '</td>' .
                                                                    '<td>' . $ticket[$i]['datepurchased'] . '</td>' .
                                                                    '</tr>';
                                                                }
                                                                ?>
                                                            </tbody>
                                                            <tfoot>
                                                                <tr>
                                                                    <td colspan="6">
                                                                        <div class="pagination pagination-centered hide-if-no-paging"></div>
                                                                    </td>
                                                                </tr>
                                                            </tfoot>
                                                        </table>
                                                    </div>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <div class="large-12 columns">
                                                        <h4>Ticket history</h4>
                                                        <hr/>
                                                        <h5 class="subheader">No ticket has been purchased recently.</h5>
                                                    </div>
                                                    <?php
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                    break;
                                case 'amend':
                                    if (isset($_SESSION['amendment'])) {
                                        ?>
                                        <input type="hidden" value="<?php echo $_SESSION['amendment']['rid']; ?>" id="route"/>
                                        <input type="hidden" value="<?php echo $_SESSION['amendment']['originid']; ?>" id="origin"/>
                                        <input type="hidden" value="<?php echo $_SESSION['amendment']['destinid']; ?>" id="destin"/>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">
                                                        <div class="row">
                                                            <div class="large-12 columns">
                                                                <div class="row">
                                                                    <div class="large-12 columns">
                                                                        <h4>Amend Ticket</h4>
                                                                        <hr/>
                                                                        <input type="hidden" id="reference" value="<?php echo $_SESSION['amendment']['referenceno']; ?>"/>
                                                                        <h5 class="subheader">Ticket Reference Number : <?php echo str_pad($_SESSION['amendment']['referenceno'], 10, '0', STR_PAD_LEFT); ?></h5>
                                                                        <br/>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                    <div class="large-4 columns">
                                                                        <label for="date" class="inline">Date of Journey</label>
                                                                    </div>
                                                                    <div class="large-8 columns">
                                                                        <input type="text" id="date" name="date" readonly/>
                                                                    </div>
                                                                </div>
                                                                <hr/>
                                                                <div class="row">
                                                                    <div class="large-12 columns">
                                                                        <input type="button" value="Submit" class="button small right" onclick="obtainSchedule();"/>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="large-12 columns">
                                                                <form id="schedule" class="custom">

                                                                </form>
                                                            </div>
                                                            <input type="hidden" id="seatid" name="seatid"/>
                                                            <div id="selectseat"class="large-12 columns">

                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                        unset($_SESSION['amendment']);
                                    } else {
                                        ?>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">

                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    break;
                                case 'cancel':
                                    if (isset($_SESSION['reference']) && isset($_SESSION['cancellation'])) {
                                        $reference = $_SESSION['reference'];
                                        $messages = $_SESSION['cancellation'];
                                        ?>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">
                                                        <h4>Ticket Cancellation</h4>
                                                        <hr/>
                                                        <div class="row">
                                                            <div class="large-12 columns">
                                                                <?php
                                                                if (!$messages['error']) {
                                                                    ?>
                                                                    <div class="row">
                                                                        <div class="columns">
                                                                            <p>Ticket Reference Number : <?php echo str_pad($reference, 10, '0', STR_PAD_LEFT); ?></p>
                                                                        </div>
                                                                    </div>
                                                                    <div class="row">
                                                                        <div class="columns">
                                                                            <p><?php echo $messages['message']; ?></p>
                                                                            <small>Amount of refund : RM<?php echo $messages['refund']; ?></small>
                                                                        </div>
                                                                    </div>
                                                                    <hr/>
                                                                    <div class="row">
                                                                        <div class="large-12 columns">
                                                                            <label class="inline">Do you want to continue?</label>
                                                                        </div>
                                                                        <div class="large-2 columns">
                                                                            <input type="button" value="Yes" class="button small left" 
                                                                                   onclick="confirmCancellation(<?php echo $reference; ?>,<?php echo $messages['refund']; ?>);"/>
                                                                        </div>
                                                                        <div class="large-10 columns">
                                                                            <input type="button" value="No" class="button small left" 
                                                                                   onclick="window.location.replace('<?php echo $_SERVER['HTTP_REFERER']; ?>');"/>
                                                                        </div>
                                                                    </div>
                                                                    <?php
                                                                } else {
                                                                    ?>
                                                                    <div class="row">
                                                                        <div class="columns">
                                                                            <p>Ticket Reference Number : <?php echo str_pad($reference, 10, '0', STR_PAD_LEFT); ?></p>
                                                                        </div>
                                                                    </div>
                                                                    <div class="row">
                                                                        <div class="columns">
                                                                            <p><?php echo $messages['message']; ?></p>
                                                                        </div>
                                                                    </div>
                                                                    <hr/>
                                                                    <div class='row'>
                                                                        <div class="large-12 columns">
                                                                            <input type="button" value="Back" class="button small left" 
                                                                                   onclick="window.location.replace('<?php echo $_SERVER['HTTP_REFERER']; ?>');"/>
                                                                        </div>
                                                                    </div>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                        unset($_SESSION['reference']);
                                        unset($_SESSION['cancellation']);
                                    } else {
                                        ?>
                                        <div class="row">
                                            <div class="large-11 large-offset-1 columns">
                                                <div class="row">
                                                    <div class="large-12 columns">

                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    break;
                            }
                            ?>
                        </div>
                        <div class="large-3 pull-9 columns panel">
                            <a href="profile.php?mode=21"><h5>User Profile</h5></a>
                            <ul class="side-nav">
                                <li><a href="profile.php?mode=22">Change password</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=23">Maintain profile</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=24">Point redemption</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=25">Reload account</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=26">Purchase ticket</a></li>
                                <li class="divider"></li>
                                <li class="active"><a href="viewticket.php?mode=viewall">View ticket history</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <?php
            } elseif (isset($_SESSION['username'])) {
                ?>
                <div class="columns">
                    <div class="row">
                        <div class="large-9 push-3 columns">
                            <div class="row">
                                <div class="large-11 large-offset-1 columns panel">
                                    You have nothing to do here, please select your ticket at <a href=''>here</a> first.
                                </div>
                            </div>
                        </div>
                        <div class="large-3 pull-9 columns panel">
                            <a href="profile.php?mode=21"><h5>User Profile</h5></a>
                            <ul class="side-nav">
                                <li><a href="profile.php?mode=22">Change password</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=23">Maintain profile</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=24">Point redemption</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=25">Reload account</a></li>
                                <li class="divider"></li>
                                <li><a href="profile.php?mode=26">Purchase ticket</a></li>
                                <li class="divider"></li>
                                <li class="active"><a href="viewticket.php?mode=viewall">View ticket history</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <?php
            } else {
                ?>
                <div class="row">
                    <div class="large-10 large-offset-1 columns panel">
                        You do not have the permission to access this page, please <a href="loginPage.php">login</a> first.
                        <?php
                        if (isset($_SERVER['HTTP_REFERER'])) {
                            echo '<br/>Click <a href="' . $_SERVER['HTTP_REFERER'] . '">here</a> to back to previous page.';
                        }
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
        <footer class="row">
            <div class="large-12 columns">
                <hr />
                <div class="row">
                    <div class="large-4 columns">
                        <p>&copy; Copyright 2013.</p>
                    </div>
                    <div class="large-6 columns">
                        <ul class="inline-list right">
                            <li><a href="aboutus.php">About Us</a></li>
                            <li><a href="contactus.php">Contact Us</a></li>
                            <li><a href="terms.php">Terms and Condition</a></li>
                    </div>
                </div>
            </div>
        </footer>
        <script src="js/foundation.min.js"></script>
        <script>
            $(document).foundation();
        </script>
    </body>
</html>